var type;

jQuery(document).ready(function() {

	type = getUrlParam('type');
	var menucode = 'index';
	
//	bindDict1('taskname','DM_BLHJ', type);

	if(type != null){
		$('.task').hide();
		menucode = type;
		$('#taskname').val(type);
	}
	else{
		menucode = 'index';
	}
	
//	alert(menucode);
	
	App.setPage(menucode);  //Set current page
	App.init(); //Initialise plugins and elements
	

	
	
	initTable();

	//列表查询事件
	$('#search').click(function(){
		$('#table').bootstrapTable(('refresh'));  // 很重要的一步，刷新url！
	});
	
	$('#startProcess').click(function(){
		var appid=new Date().getTime();
		var data = {"appid": appid};

		$.ajax({ 
			method: 'post',
			url: basePath() + 'business/saveForm.do',
			data: JSON.stringify(data),
			dataType: "json",
	        contentType: "application/json",
			success: function(data){
				$('#table').bootstrapTable(('refresh'));  // 很重要的一步，刷新url！	

	      	}
		});
	});
	
	$('#save').click(function(){
		$(this).prop('disabled', true);
		
		$("#ifModal")[0].contentWindow.save();
	});
});		



function initTable(){		
	$('#table').bootstrapTable({
		method: 'post',
	    url: basePath() + 'index/tasklist.do',
//	    pagination: true, //分页
	    dataType: "json",
        contentType: "application/json",
//        pageList: [10, 20, 50, 100],  //可供选择的每页的行数（*）
        striped: true,      //是否显示行间隔色
        sidePagination: "server",
        sortOrder: "desc",
        sortName: 'sendtime',//排序字段
        sortable: true,
        ////查询参数,每次调用是会带上这个参数，可自定义                         
        queryParams:function(params) {
        	var taskname = $('#taskname').val();
        	if(type != null){
        		taskname = type;
        	}
        	console.log(params);
            return {
                taskname: taskname,
                itemname : $('#itemname').val(),
                sortName: params.sort,      //排序列名  
                sortOrder: params.order //排位命令（desc，asc）
            };
        },
	    columns: [{
//            field : 'checked',
//            checkbox : true
//
//        }, {  
            field : 'number',  
            title : '序号',  
            align : 'center',
            width : '80px',
            formatter : function(value, row, index) {  
// 		                return index + 1;  
                var page = $('#table').bootstrapTable("getPage");  
                return page.pageSize * (page.pageNumber - 1) + index + 1;  
            }  
        }, {
	        field: 'itemname',
	        title: '任务名称',
            align : 'left',
            sortable: true,
            formatter : function(value, row, index) {  
		        return '<a href="javascript:void(0)"  onclick=\'jumpTask('+ JSON.stringify(row) +')\'>' + row.applyno + ' - ' + row.itemname + ' - ' + row.orgname + '</a>';  
		    }
	    },  {
	    	field: 'taskname',
	    	title: '当前环节'
	    }, {
	        field: 'sendtime',
	        title: '申报时间',
	        sortable: true,
            formatter : function(value, row, index) {  
		        return formatDate(value, 'yyyy-mm-dd hh:mi');  
		    }
	    }, {
	        field: 'company',
	        title: '申报单位'
	    }]
	});
}

function operateFormatter(value, row, index) {
    return [            
            '<button type="button" class="ShowImage btn btn-info  btn-sm" style="margin-right:10px;"><span class="glyphicon glyphicon-eye-open"></span>流程图</button>'
    ].join('');
}

function jumpTask(data){
	setModalSize();
	if(data.taskurl!=undefined&&data.taskurl!=''){
		$('#ifModal').attr("src", data.taskurl+"?applyno=" + data.applyno + '&instanceid=' + data.instanceid + '&flowtaskid=' + data.flowtaskid+ '&applyid=' + data.applyid+ '&pinstanceid=' + data.pinstanceid+ '&taskuserid=' + data.taskuserid);
	}else{
		$('#ifModal').attr("src", "backstage/approval/business/accept.html?applyno=" + data.applyno + '&instanceid=' + data.instanceid + '&flowtaskid=' + data.flowtaskid+ '&applyid=' + data.applyid+ '&pinstanceid=' + data.pinstanceid+ '&taskuserid=' + data.taskuserid);
	}
	
	$('#modalTile').html(data.applyno + ' - ' + data.itemname + ' - ' + data.orgname + ' - ' + data.taskname);
	$("#modal").modal('show');

	$('#save').prop('disabled', false);
}

window.operateEvents = {
    'click .ShowImage': function (e, value, row, index) {      	
		var taskId = row.taskId;

		$.ajax({ 
//			method: 'post',
			url: basePath() + 'index/findflowpoint.do',
			data: {"taskId": taskId},
			dataType: "json",
	        contentType: "application/json",
			success: function(data) {
				if(data.result == 1) {
			    	window.open('flowimg.html?taskId=' + taskId + '&x=' + data.datas.x + '&y=' + data.datas.y + '&width=' + data.datas.width + '&height=' + data.datas.height,'top');
				}
	      	}
		});
    	
    	
    }
 };

 function putUser(data){
	 ifModal.window.addUser(data);
 }
 function modeOut(){
	$('#customUser').attr('src', "customcontrol/customuser/index.html");
	
	var documentW = $(window).width();
	var documentH = $(window).height();
	$('#cmodalW').width(documentW * 0.8);
	$('#cmodalH').height(documentH * 0.8);
	$('#customeMode').modal('show');
 }

 function savedisabled(b){
 	$('#save').prop('disabled', b);
 }